<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/5/16
 * Time: 16:22
 */

namespace Mengwu\Controller;

use Admin\Api\ViewPageApi;
use Mengwu\Api\MwApi;
use Shop\Api\BannersApi;

class IndexController extends BaseStoreController{

    public function index(){
        $this->assignTitle('买母婴上萌屋');
        $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
        //获取轮播图
        $result = apiCall(BannersApi::QUERY_NO_PAING,[['position'=>6093]]);

        if($result['status']){
            $this->assign('banner',$result['info']);
        }

        //获取特卖专场
        $map = [
            'name' => '特卖专场',
            'notes' => 'PC首页特卖专场布局'
        ];
        $result = apiCall(ViewPageApi::GET_PAGE_INFO,[$map]);
        $zhuanchang = [];
        if($result['status']){
            $page = $result['info'];
            $items = $page['item_data'];
            foreach($items as $val){
                $item_data = json_decode($val['data'],true);
                $banners = $item_data['banners'][0];
                $zhuanchang[] = [
                    'block_id' => $item_data['block_id'],
                    'banner' => $PICTURE_REMOTE_URL.$banners['id'],
                    'title' => $banners['title'],
                    'desc' => $banners['desc'],
                    'discount' => $banners['discount'],
                    'main_time' => $banners['group_main_time']
                ];
            }
            $this->assign('zhuanchang',$zhuanchang);
        }

        //获取主题活动
        $map = [
            'name' => '主题活动',
            'notes' => 'PC首页主题活动布局'
        ];
        $result = apiCall(ViewPageApi::GET_PAGE_INFO,[$map]);
        $zhutihuodong = [];
        if($result['status']){
            $page = $result['info'];
            $items = $page['item_data'];
            foreach($items as $val){
                $item_data = json_decode($val['data'],true);
                $banners = $item_data['banners'][0];
                $zhutihuodong[] = [
                    'block_id' => $item_data['block_id'],
                    'banner' => $PICTURE_REMOTE_URL.$banners['id'],
                    'main_time' => $banners['group_main_time']
                ];
            }
            $this->assign('zhutihuodong',$zhutihuodong);
        }

        $this->boye_display();

    }

    /**
     * 搜索
     */
    public function search(){
        $s = I('s');
        $this->assign('search_key',$s);
        $this->assignTitle('搜索'.$s);

        $page_no = I('p',1);
        $page_size = 16;
        $order = I('order',10);
        if(!in_array($order,[10,18,11])){
            $order = 10;
        }

        if(!empty($s)){

            $data = [
                'name' => $s,
                'page_no' => $page_no,
                'page_size' => $page_size,
                'order' => $order
            ];

            $result = mwApi(MwApi::BY_Product_query,$data);

            if($result['status']){
                $product_list = $result['info']['list'];
                $PICTURE_REMOTE_URL = C('PICTURE_REMOTE_URL');
                foreach($product_list as &$val){
                    $val['img'] = $PICTURE_REMOTE_URL.$val['img'].'&size=240';
                }
                $show = paging($page_no,$page_size,$result['info']['count'],'mengwu/index/search');
                $this->assign('show',$show);
                $this->assign('product_list',$product_list);
            }

        }
        $this->boye_display();
    }

    
}